package com.ecom.common.controller;

import com.ecom.common.dao.ProductDao;
import com.ecom.common.model.Book;
import com.ecom.common.model.Movie;
import com.ecom.common.service.ProductService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/welcome")
public class ProductController {

    private static final Logger logger = Logger.getLogger(ProductController.class);

    @Autowired
    ProductService productService;


    @RequestMapping(method = RequestMethod.GET)
	public String printWelcome(ModelMap model) {

        int value = productService.checkDatabase();

        logger.debug(productService.getCategories().get(0).getCategoryName());

        logger.debug(productService.getSubCategories(productService.getCategories().get(0)).get(0).getSubCategoryName());

        logger.debug(((Book) (productService.getProductFromCategory("book", productService.getCategories().get(0)).get(0))).getAuthor());

        logger.debug(((Movie)(productService.getProductFromCategory("movie", productService.getCategories().get(0)).get(0))).getDirector());

        logger.debug(((Movie)(productService.getProductFromSubCategory("movie", productService.getSubCategories(productService.getCategories().get(0)).get(0)).get(0))).getDirector());

        logger.debug(((Book) (productService.getProductFromSubCategory("book", productService.getSubCategories(productService.getCategories().get(0)).get(1)).get(0))).getAuthor());


        model.addAttribute("message", "Spring 3 MVC Hello World" + String.valueOf(value));
		return "hello";

	}
	
}